<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_big'"
      title="合并检测"
      align="center"
      :visible.sync="innerDialogFormVisible"
      @closed="closeDialog"
    >
      <div>
        <el-form ref="queryForm" :model="queryForm" inline>
          <el-form-item label="样品编号" prop="sampleNum">
            <el-input
              v-model="queryForm.sampleNum"
              placeholder="样品编号"
              clearable
              @keyup.enter.native="onQuery()"
            />
          </el-form-item>
          <el-form-item label="委托单号" prop="orderNum">
            <el-input
              v-model="queryForm.orderNum"
              placeholder="委托单号"
              clearable
              @keyup.enter.native="onQuery()"
            />
          </el-form-item>
          <el-form-item label="检测项目" prop="detectionItemName">
            <el-input
              v-model="queryForm.detectionItemName"
              placeholder="检测项目"
              clearable
              @keyup.enter.native="onQuery()"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onQuery">查询</el-button>
            <el-button @click="cancelForm">清空</el-button>
          </el-form-item>
        </el-form>
        <!--操作栏-->
        <div class="base-content-operate">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="entrustTaskDistributionFun"
            >任务分配</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="voluntarilyAllocation"
            >自动分配</el-button
          >
        </div>
        <el-table
          :data="tableData"
          tooltip-effect="dark"
          height="340px"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column label="委托单号">
            <template slot-scope="scope">
              {{ scope.row.orderNum }}
            </template>
          </el-table-column>
          <el-table-column label="样品编号">
            <template slot-scope="scope">
              {{ scope.row.sampleNum }}
            </template>
          </el-table-column>
          <el-table-column label="样品名称">
            <template slot-scope="scope">
              {{ scope.row.sampleName }}
            </template>
          </el-table-column>
          <el-table-column prop="inspectedUnit" label="受检单位">
          </el-table-column>
          <el-table-column label="样品类别">
            <template slot-scope="scope">
              {{ scope.row.sampleCategoryName }}
            </template>
          </el-table-column>

          <el-table-column label="检测项目">
            <template slot-scope="scope">
              {{ scope.row.detectionItemName }}
            </template>
          </el-table-column>
        </el-table>

        <BasePagination
          :total="queryForm.total"
          :page-index="queryForm.pageIndex"
          :page-size="queryForm.pageSize"
          :page-count="pageCount"
          @sizeChange="sizeChange"
          @changePage="currentChange"
        />
      </div>

      <VoluntarilyAllocationTaskDialog
        v-if="dialogVoluntarily"
        ref="VoluntarilyAllocationTaskDialog"
        :dialog-voluntarily.sync="dialogVoluntarily"
        :task-id-list.sync="taskIdList"
        @close="voluntarilyAllocationTaskClose"
      />

      <EntrustTaskDistribution
        v-if="dialogFormVisible"
        ref="EntrustTaskDistribution"
        :data="tableDatas"
        :dialog-form-visible.sync="dialogFormVisible"
        @getList="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import VoluntarilyAllocationTaskDialog from "./VoluntarilyAllocationTaskDialog.vue";
import { GetEntrustTaskSampleDetection } from "../../api/EntrustTaskManage/EntrustTask";
import EntrustTaskDistribution from "../../views/EntrustTaskManage/EntrustTask/EntrustTaskDistribution.vue";
export default {
  name: "PooledDetectionDialog",
  components: { EntrustTaskDistribution, VoluntarilyAllocationTaskDialog },
  props: {
    dialogFormPooledDetectionVisible: {
      type: Boolean, // 布尔
      default: false, // 必填
    },
    // 委托单id和样品登记id集合
    entrustOrderSampleRegistrationIdList: {
      type: Array,
      default: () => [],
    },
    entrustTaskIdList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      taskIdList: [],
      dialogVoluntarily: false,
      tableData: [],
      tableDatas: [],
      dialogFormVisible: false,
      innerDialogFormVisible: false,
      loading: false,
      pageCount: 0,
      queryForm: {
        sampleNum: "",
        orderNum: "",
        detectionItemName: "",
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        entrustOrderSampleRegistrationIdList: [],
        entrustTaskIdList: [],
      },
    };
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormPooledDetectionVisible;
    this.queryForm.entrustOrderSampleRegistrationIdList =
      this.entrustOrderSampleRegistrationIdList;
    this.queryForm.entrustTaskIdList = this.entrustTaskIdList;
    this.getList();
  },
  methods: {
    voluntarilyAllocationTaskClose() {
      this.getList();
    },
    // 自动分配
    voluntarilyAllocation() {
      if (this.tableDatas === null || this.tableDatas.length === 0) {
        this.$message.error("请选择数据");
        return false;
      }
      for (let i = 0; i < this.tableDatas.length; i++) {
        this.taskIdList.push(this.tableDatas[i].entrustTaskId);
      }
      this.dialogVoluntarily = true;
    },
    // 清空筛选条件
    cancelForm() {
      this.queryForm.sampleNum = "";
      this.queryForm.orderNum = "";
      this.queryForm.detectionItemName = "";
      this.sizeChange();
    },
    onQuery() {
      this.sizeChange();
    },
    getList() {
      GetEntrustTaskSampleDetection(this.queryForm).then((res) => {
        this.tableData = res.data.dataList ? res.data.dataList : [];
        this.queryForm.total = res.data.total;
      });
    },
    // 任务分配
    entrustTaskDistributionFun() {
      if (this.tableDatas === null || this.tableDatas.length === 0) {
        this.$message.error("请选择数据");
        return false;
      }

      this.dialogFormVisible = true;
    },
    closeDialog() {
      this.innerDialogFormVisible = false;
      this.$emit("update:dialogFormPooledDetectionVisible", false);
      this.$emit("getList");
    },
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
    currentChange(val) {
      this.queryForm.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.queryForm.pageIndex = 1;
      this.queryForm.pageSize = val;
      this.getList();
    },
  },
};
</script>
